package com.tcsl.mylibrary.utils;

import android.content.Intent;
import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by LukeSkywalker on 2016/11/26.
 * 此类要和发给第三方的类一致
 */
public class FFResult {
    // ============== ALL EXTRA ================
    public static final String EXTRA_ORDER_NO = "extra_order_no";
    public static final String EXTRA_AMOUNT = "extra_amount";
    public static final String EXTRA_PAY_RESULT = "extra_pay_result";
    public static final String EXTRA_PAY_ORDER_NO = "extra_pay_order_no";
    public static final String EXTRA_PAY_TYPE = "extra_pay_type";
    public static final String EXTRA_POS_ACCOUNT = "extra_pos_account";
    public static final String EXTRA_REFUND_ORDER_NO = "extra_refund_order_no";
    public static final String EXTRA_TRADE_STATUS = "extra_trade_status";
    public static final String EXTRA_TRADE_TYPE = "extra_trade_type";
    public static final String EXTRA_TRADE_PAY_RESULT = "extra_trade_pay_result";
    public static final String EXTRA_TRADE_REFUND_RESULT = "extra_trade_refund_result";
    public static final String EXTRA_CODE = "extra_code";
    public static final String EXTRA_EXCHANGE_TYPE = "extra_exchange_type";
    public static final String EXTRA_PRODUCT_NO = "extra_product_no";
    public static final String EXTRA_PRODUCT_NAME = "extra_product_name";
    public static final String EXTRA_PHONE = "extra_phone";
    public static final String EXTRA_MEMBER_ID = "extra_member_id";

    // ====================================支付类型===================================
    /**
     * 现金
     */
    public static final int PAY_TYPE_CASH = 2001;
    /**
     * 公交卡
     */
    public static final int PAY_TYPE_METRO_CARD = 2002;
    /**
     * 飞凡卡
     */
    public static final int PAY_TYPE_FEIFAN_CARD = 2004;
    /**
     * 银行卡
     */
    public static final int PAY_TYPE_BANK_CARD = 2005;
    /**
     * 积分
     */
    public static final int PAY_TYPE_POINT = 2006;
    /**
     * 微信
     */
    public static final int PAY_TYPE_WECHAT = 1404;
    /**
     * 快钱
     */
    public static final int PAY_TYPE_KUAIQIAN = 1802;
    /**
     * 飞凡付款码
     */
    public static final int PAY_TYPE_FEIFAN_CODE = 1101;

    // ====================================支付结果===================================
    /**
     * 支付结果 支付成功
     */
    public static final int RESULT_SUCCESS = 0;
    /**
     * 支付结果 支付失败
     */
    public static final int RESULT_FAILURE = 1;
    /**
     * 支付结果 支付关闭
     */
    public static final int RESULT_CLOSE = 2;

    // ===================================查询结果====================================
    /**
     * 查询失败
     */
    public static final int TRADE_QUERY_STATU_FAIL = 0;
    /**
     * 查询成功
     */
    public static final int TRADE_QUERY_STATU_SUCCESS = 1;
    /**
     * 未知交易类型，查询失败时会是此种状态
     */
    public static final int TRADE_TYPE_UNKNOWN = -1;
    /**
     * 支付
     */
    public static final int TRADE_TYPE_PAY = 0;
    /**
     * 退款
     */
    public static final int TRADE_TYPE_REFUND = 1;

    /**
     * 支付结果数据
     */
    public static class PayResult implements Parcelable {

        // ========================================================================
        /**
         * 支付结果 0成功{@link #RESULT_SUCCESS} 1失败{@link #RESULT_FAILURE} 2关闭{@link #RESULT_CLOSE}
         */
        public final int mResult;
        /**
         * 合作方支付时订单号
         */
        public final String mOrderNo;
        /**
         * POS账号
         */
        public final String mPosAccount;
        /**
         * POS支付单号
         */
        public final String mPayOrderNo;
        /**
         * POS支付类型 参考对应支付类型数据
         */
        public final int mPayType;
        /**
         * POS支付金额 以分为单位，如100.01元，则应返回10001
         */
        public final long mAmount;

        public PayResult(Intent data) {
            mResult = data.getIntExtra(EXTRA_PAY_RESULT, 0);
            mOrderNo = data.getStringExtra(EXTRA_ORDER_NO);
            mPosAccount = data.getStringExtra(EXTRA_POS_ACCOUNT);
            mPayOrderNo = data.getStringExtra(EXTRA_PAY_ORDER_NO);
            mPayType = data.getIntExtra(EXTRA_PAY_TYPE, 0);
            mAmount = data.getLongExtra(EXTRA_AMOUNT, 0);
        }

        public PayResult(int result, String orderNo, String posAccount, String payOrderNo, int
                payType, long amount) {
            mResult = result;
            mOrderNo = orderNo;
            mPosAccount = posAccount;
            mPayOrderNo = payOrderNo;
            mPayType = payType;
            mAmount = amount;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(this.mResult);
            dest.writeString(this.mOrderNo);
            dest.writeString(this.mPosAccount);
            dest.writeString(this.mPayOrderNo);
            dest.writeInt(this.mPayType);
            dest.writeLong(this.mAmount);
        }

        protected PayResult(Parcel in) {
            this.mResult = in.readInt();
            this.mOrderNo = in.readString();
            this.mPosAccount = in.readString();
            this.mPayOrderNo = in.readString();
            this.mPayType = in.readInt();
            this.mAmount = in.readLong();
        }

        public static final Creator<PayResult> CREATOR = new Creator<PayResult>() {
            @Override
            public PayResult createFromParcel(Parcel source) {
                return new PayResult(source);
            }

            @Override
            public PayResult[] newArray(int size) {
                return new PayResult[size];
            }
        };
    }

    /**
     * 退款结果数据
     */
    public static class RefundResult implements Parcelable {
        /**
         * POS账号
         */
        public final String mPosAccount;
        /**
         * POS支付单号
         */
        public final String mPayOrderNo;
        /**
         * POS退款单号
         */
        public final String mRefundOrderNo;
        /**
         * POS退款金额（有可能与原订单支付不一致，以该返回金额为实际退款金额） 以分为单位，如100.01元，则应返回10001()
         */
        public final long mAmount;

        public RefundResult(Intent data) {
            mPosAccount = data.getStringExtra(EXTRA_POS_ACCOUNT);
            mPayOrderNo = data.getStringExtra(EXTRA_PAY_ORDER_NO);
            mRefundOrderNo = data.getStringExtra(EXTRA_REFUND_ORDER_NO);
            mAmount = data.getLongExtra(EXTRA_AMOUNT, 0);
        }

        public RefundResult(String posAccount, String payOrderNo, String refundOrderNo, long
                amount) {
            mPosAccount = posAccount;
            mPayOrderNo = payOrderNo;
            mRefundOrderNo = refundOrderNo;
            mAmount = amount;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(this.mPosAccount);
            dest.writeString(this.mPayOrderNo);
            dest.writeString(this.mRefundOrderNo);
            dest.writeLong(this.mAmount);
        }

        protected RefundResult(Parcel in) {
            this.mPosAccount = in.readString();
            this.mPayOrderNo = in.readString();
            this.mRefundOrderNo = in.readString();
            this.mAmount = in.readLong();
        }

        public static final Creator<RefundResult> CREATOR = new Creator<RefundResult>() {
            @Override
            public RefundResult createFromParcel(Parcel source) {
                return new RefundResult(source);
            }

            @Override
            public RefundResult[] newArray(int size) {
                return new RefundResult[size];
            }
        };
    }

    /**
     * 查询结果数据
     */
    public static class TradeQueryResult implements Parcelable {
        /**
         * 0查询失败{@link #TRADE_QUERY_STATU_FAIL}，1查询成功{@link #TRADE_QUERY_STATU_SUCCESS}；
         */
        public final int mStatus;
        /**
         * 交易类型
         * 0支付{@link #TRADE_TYPE_PAY}，1退款{@link #TRADE_TYPE_REFUND}，默认-1{@link #TRADE_TYPE_UNKNOWN}；
         */
        public final int mTradeType;
        /**
         * 支付结果 如果交易类型为1，则该值为null，否则为支付结果数据；如果查询失败则为null
         */
        public final PayResult mPayResult;
        /**
         * 退款结果 如果交易类型为0，则该值为null，否则为退款结果数据；如果查询失败则为null
         */
        public final RefundResult mRefundResult;

        public TradeQueryResult(Intent data) {
            mStatus = data.getIntExtra(EXTRA_TRADE_STATUS, TRADE_QUERY_STATU_FAIL);
            mTradeType = data.getIntExtra(EXTRA_TRADE_TYPE, TRADE_TYPE_UNKNOWN);
            mPayResult = data.getParcelableExtra(EXTRA_TRADE_PAY_RESULT);
            mRefundResult = data.getParcelableExtra(EXTRA_TRADE_REFUND_RESULT);
        }

        public TradeQueryResult(int status, int tradeType, PayResult payResult, RefundResult
                refundResult) {
            mStatus = status;
            mTradeType = tradeType;
            mPayResult = payResult;
            mRefundResult = refundResult;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(this.mStatus);
            dest.writeInt(this.mTradeType);
            dest.writeParcelable(this.mPayResult, flags);
            dest.writeParcelable(this.mRefundResult, flags);
        }

        protected TradeQueryResult(Parcel in) {
            this.mStatus = in.readInt();
            this.mTradeType = in.readInt();
            this.mPayResult = in.readParcelable(PayResult.class.getClassLoader());
            this.mRefundResult = in.readParcelable(RefundResult.class.getClassLoader());
        }

        public static final Creator<TradeQueryResult> CREATOR = new Creator<TradeQueryResult>() {
            @Override
            public TradeQueryResult createFromParcel(Parcel source) {
                return new TradeQueryResult(source);
            }

            @Override
            public TradeQueryResult[] newArray(int size) {
                return new TradeQueryResult[size];
            }
        };
    }

    /**
     * 核销结果数据
     */
    public static class ExchangeResult implements Parcelable {
        /**
         * 优惠劵
         */
        public static final int EXCHANGE_TYPE_COUPON = 1;
        /**
         * 提货码
         */
        public static final int EXCHANGE_TYPE_VOUCHER = 2;
        /**
         * 核销码
         */
        public final String mCode;
        /**
         * 核销类型
         */
        public final int mType;
        /**
         * 商品编码
         */
        public final String mProductNo;
        /**
         * 商品名称
         */
        public final String mProductName;

        public ExchangeResult(Intent data) {
            mCode = data.getStringExtra(EXTRA_CODE);
            mType = data.getIntExtra(EXTRA_EXCHANGE_TYPE, TRADE_TYPE_UNKNOWN);
            mProductNo = data.getStringExtra(EXTRA_PRODUCT_NO);
            mProductName = data.getStringExtra(EXTRA_PRODUCT_NAME);
        }

        public ExchangeResult(String code, int type, String productNo, String productName) {
            mCode = code;
            mType = type;
            mProductNo = productNo;
            mProductName = productName;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(this.mCode);
            dest.writeInt(this.mType);
            dest.writeString(this.mProductNo);
            dest.writeString(this.mProductName);
        }

        protected ExchangeResult(Parcel in) {
            this.mCode = in.readString();
            this.mType = in.readInt();
            this.mProductNo = in.readString();
            this.mProductName = in.readString();
        }

        public static final Creator<ExchangeResult> CREATOR = new Creator<ExchangeResult>() {
            @Override
            public ExchangeResult createFromParcel(Parcel source) {
                return new ExchangeResult(source);
            }

            @Override
            public ExchangeResult[] newArray(int size) {
                return new ExchangeResult[size];
            }
        };
    }

    /**
     * 注册会员结果数据
     */
    public static class RegisterResult implements Parcelable {
        /**
         * 会员飞凡ID
         */
        public final String mMemberId;
        /**
         * 会员手机号
         */
        public final String mPhone;

        public RegisterResult(Intent data) {
            mMemberId = data.getStringExtra(EXTRA_MEMBER_ID);
            mPhone = data.getStringExtra(EXTRA_PHONE);
        }

        public RegisterResult(String memberId, String phone, String productName) {
            mMemberId = memberId;
            mPhone = phone;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(this.mMemberId);
            dest.writeString(this.mPhone);
        }

        protected RegisterResult(Parcel in) {
            this.mMemberId = in.readString();
            this.mPhone = in.readString();
        }

        public static final Creator<RegisterResult> CREATOR = new Creator<RegisterResult>() {
            @Override
            public RegisterResult createFromParcel(Parcel source) {
                return new RegisterResult(source);
            }

            @Override
            public RegisterResult[] newArray(int size) {
                return new RegisterResult[size];
            }
        };
    }
}

// ============================= JSON DATA ==============================

////////////////////////////// Exchange ///////////////////////////////
//"couponNo": "026601355650",
//        "productNo": "20160922160459",
//        "specNo": "20160922160459",
//        "productType": 1002,
//        "title": "CW有价兑换券云POS（无补贴）",
//        "subTitle": "CW有价兑换券云POS（无补贴）",
//        "couponStatus": 4,
//        "isExpired": 1,
//        "orderNo": "10110831429882",
//        "memberId": "15000000000989882",
//        "validStartTime": 1475994103000,
//        "validEndTime": 1476253303000,
//        "buyTime": 1475994103000,
//        "useTime": 1475995973000,
//        "topPics": "[\"T10CDTBbhv1RCvBVdK\"]",
//        "origPrice": "0.00",
//        "salePrice": "10.00",
//        "settlementInfo": {
//        "salePrice": "1000"
//        }
//
//
//        "productId": "61360",
//        "productCount": 1,
//        "productPrice": "5",
//        "storeId": "10010796",
//        "merchantId": "10025604",
//        "plazaId": "1001015",
//        "productInfo": "{\"attribute\":\"M\",\"consumedRefundDays\":7,\"consumedRefundFlag\":1,
// \"merchantCutAmount\":\"38\",\"operateCutAmount\":\"5\",\"parentId\":\"1000056913\",
// \"pic\":\"T1pXxTBbAT1RCvBVdK\",\"price\":\"48\",\"priceBeforePromotion\":48,
// \"redundancy03\":\"0\",\"spacId\":\"61360\",\"title\":\"V21时尚魅力色彩内裤\",\"tradeCode\":\"7013\",
// \"useValidBegin\":\"\",\"useValidDays\":3,\"useValidEnd\":\"\",\"useValidType\":0}",
//        "title": "V21时尚魅力色彩内裤",
//        "picture": "T1pXxTBbAT1RCvBVdK",
//        "merchantCutAmount": "38",
//        "operateCutAmount": "5",
//        "priceBeforePromotion": "48",
//        "applyRefundFlag": 0,
//        "refundFinishedFlag": 0
